#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "GameLayer.h"

#include "cocos-ext.h"
#include "VisibleRect.h"
#pragma  comment(lib,"libBox2d.lib")
#pragma  comment(lib,"libExtensions.lib")
using namespace extension;
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    CCSprite* bg = CCSprite::create("bg.jpg");
    bg->setPosition(ccp(size.width/2, size.height/2));
    this->addChild(bg, -3);
    
    CCSprite* tree1 = CCSprite::create("m2.png");
    tree1->setAnchorPoint(ccp(0, 1));
    tree1->setPosition(ccp(27, 426));
    this->addChild(tree1, -2);
    
    CCSprite* tree2 = CCSprite::create("m1.png");
    tree2->setAnchorPoint(ccp(1, 1));
    tree2->setPosition(ccp(635, 351+309-218));
    this->addChild(tree2, -2);
    
    CCSprite* grass = CCSprite::create("grass.png");
    grass->setAnchorPoint(ccp(0.5, 1));
    grass->setPosition(ccp(size.width/2, 289));
    this->addChild(grass, -1);

    CCSprite* ground = CCSprite::create("ground.png");
    ground->setAnchorPoint(ccp(0.5f, 0));
    ground->setPosition(ccp(size.width/2, 0));
    this->addChild(ground, 1);
    
    CCSprite *cloud4 = CCSprite::create("cloud4.png");
    cloud4->setPosition(ccp(499, 999));
    cloud4->setAnchorPoint(ccp(0, 0));
    this->addChild(cloud4);
    
    CCSprite *cloud3 = CCSprite::create("cloud3.png");
    cloud3->setPosition(ccp(97, 527));
    cloud3->setAnchorPoint(ccp(1, 0));
    this->addChild(cloud3, -1);
    
    CCSprite *cloud2 = CCSprite::create("cloud2.png");
    cloud2->setPosition(ccp(144, 767));
    cloud2->setAnchorPoint(ccp(1, 0));
    this->addChild(cloud2, -1);
    
    CCSprite *cloud1 = CCSprite::create("cloud1.png");
    cloud1->setPosition(ccp(106, 963));
    cloud1->setAnchorPoint(ccp(0, 0));
    this->addChild(cloud1);
    
    CCSprite *gameName = CCSprite::create("title.png");
    gameName->setPosition(ccp(size.width/2, 921));
    gameName->setAnchorPoint(ccp(0.5, 1));
    this->addChild(gameName);
    
    //flypig
    CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("birdAction0.png", "birdAction0.plist", "birdAction.json");
    
	cocos2d::extension::CCArmature *armature = NULL;
	armature = cocos2d::extension::CCArmature::create("birdAction");
	armature->getAnimation()->playByIndex(0);
    //armature->setAnchorPoint(ccp(0.5, -0.1));
	//armature->setScale(0.2f);
	armature->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y+25));
	addChild(armature);

    CCActionInterval*  jump = CCJumpBy::create(2, ccp(0, 0), 50, 1);
    armature->runAction(CCRepeatForever::create(jump));
    
    
    CCMenuItem* itemPlay = CCMenuItemImage::create("play.png", "play.png", this, menu_selector(HelloWorld::menuPlayCallback));
    itemPlay->setPosition(ccp(size.width/2-45, 343));
    itemPlay->setAnchorPoint(ccp(0.5, 0));
    
    CCActionInterval*  actionTo = CCScaleBy::create(1, 1.1, 0.95);
    CCFiniteTimeAction*  seqAction1 = CCSequence::create( actionTo, actionTo->reverse(), NULL);
    
    CCActionInterval*  moveBy = CCMoveBy::create(1, ccp(0, -15));
    CCFiniteTimeAction*  seqAction = CCSequence::create( moveBy, moveBy->reverse(), NULL);


    itemPlay->runAction(CCRepeatForever::create(
                                                CCSpawn::create(seqAction1,
                                                                seqAction,
                                                                NULL)
                                                )
                        );
    
    /*CCMenuItem* itemGc = CCMenuItemImage::create("gc.png", "gc.png", this, menu_selector(HelloWorld::menuGcCallback));
    itemGc->setPosition(ccp(345, 343));
    itemGc->setAnchorPoint(ccp(0, 0));*/
    
    CCMenuItem* itemRate = CCMenuItemImage::create("rate.png", "rate.png", this, menu_selector(HelloWorld::menuRateCallback));
    itemRate->setPosition(ccp(size.width/2, 332));
    itemRate->setAnchorPoint(ccp(0.5, 1));

    
    CCMenu* menuPlay = CCMenu::create(itemPlay, itemRate, NULL);
    menuPlay->setPosition(CCPointZero);
    this->addChild(menuPlay);

    return true;
}

void HelloWorld::menuPlayCallback(){
    CCLOG("Play");
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("click.mp3");
    CCScene *scene = CCScene::create();
    scene->addChild(GameLayer::create());
    
    CCDirector::sharedDirector()->replaceScene( CCTransitionFade::create(1, scene) );
}

void HelloWorld::menuGcCallback(){
    CCLOG("Gc");
}

void HelloWorld::menuRateCallback(){
    CCLOG("Rate");
}